package com.sevencar.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.lightble.R;
import com.example.lightble.db.SettingDatabase;


public class SplashActivity extends Activity implements OnPageChangeListener{
	
	private ArrayList<View> views;
	Button intobtn;
	Button point1;
	Button point2;
	Button point3;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash);
		
		
		
		
		 SettingDatabase sdb = new SettingDatabase(this,0);
		 sdb.execSQL("delete  from   table_temp_device ");
		 sdb.close();
		 
		
		int[] images = new int[] { R.drawable.s1,
				R.drawable.s3, R.drawable.s2 };
		views = new ArrayList<View>();
		ViewPager viewPager = (ViewPager) findViewById(R.id.viewpage);
		intobtn = (Button)findViewById(R.id.intobtn);
		intobtn.setVisibility(View.INVISIBLE);
		point1 = (Button)findViewById(R.id.point1);
		point2 = (Button)findViewById(R.id.point2);
		point3 = (Button)findViewById(R.id.point3);
		point1.setSelected(true);
		
		intobtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				Intent i = new Intent(SplashActivity.this,MainActivity.class);
				startActivity(i);
				finish();
			}
		});
		for (int i = 0; i < images.length; i++) {
			ImageView imageView = new ImageView(this);
			imageView.setBackgroundResource(images[i]);
			views.add(imageView);
		}
		
		BasePagerAdapter pagerAdapter = new BasePagerAdapter(views);
		viewPager.setAdapter(pagerAdapter); 
		viewPager.setOnPageChangeListener(this);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		
	}

	@Override
	public void onPageSelected(int arg0) {
		if(arg0==2){
			intobtn.setVisibility(View.VISIBLE);
		}else {
			intobtn.setVisibility(View.INVISIBLE);
		}
		point1.setSelected(false);
		point2.setSelected(false);
		point3.setSelected(false);
		if(arg0==0){
			point1.setSelected(true);
		}else if(arg0==1) {
			point2.setSelected(true);
		}else if(arg0==2) {
			point3.setSelected(true);
		}
		
		
	}
	
	
	public class BasePagerAdapter extends PagerAdapter{
		private List<View> views=new ArrayList<View>();
		
		public BasePagerAdapter(List<View> views){
			this.views=views;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public int getCount() {
			return views.size();
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			((ViewPager) container).removeView(views.get(position));
		}

		@Override
		public Object instantiateItem(View container, int position) {
			((ViewPager) container).addView(views.get(position));
			return views.get(position);
	}}

	
	
}